package cn.itcast.controller;


import cn.itcast.util.Result;
import cn.itcast.util.SysConfig;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.IIOException;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("file")
@CrossOrigin
public class FileUploadController {
    @RequestMapping("upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {

        //将文件上传到指定位置
        //文件上传时的名字
        String oldname=file.getOriginalFilename();
        String lastname=oldname.substring(oldname.lastIndexOf("."));//.jpg .txt等截取字符串
        String newname= UUID.randomUUID().toString()+lastname;//xxxxxx.jpeg
        File target=new File(SysConfig.UPLOAD_DIR+'/'+newname);
        file.transferTo(target);
        //返回文件访问路径
        String path=SysConfig.UPLOAD_PATH+newname;
        return Result.success("上传成功",path);
    }
}
